
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class BarView : MonoBehaviour
{
    public static BarView Instance;
    public enum PropType{Unuse,Remove,Undo,Refresh}
    [SerializeField] public PropType propType;
    [SerializeField] GameObject popView;
    [SerializeField] GameObject removeView;
    [SerializeField] GameObject undoView;
    [SerializeField] GameObject refreshView;
    [Space]
    [SerializeField] GameObject removePlus;
    [SerializeField] GameObject undoPlus;
    [SerializeField] GameObject refreshPlus;
    [Space]
    [SerializeField] int removePropCount;
    [SerializeField] int undoPropCount;
    [SerializeField] int refreshPropCount;
    [SerializeField] Sprite oneSprite;
    [SerializeField] Sprite plusSprite;
    [SerializeField] GameObject removemask;
    [SerializeField] GameObject undomask;
    [SerializeField] GameObject refresgmask;
    [SerializeField] GameObject propLayer;
    [SerializeField] public bool isRefresh;
    [Space]
    [SerializeField] GameObject one;
    [SerializeField] GameObject two;
    [Space]
    [SerializeField] GameObject emptyBoxPrefab;

    private void Awake()
    {
        Instance = this;
    }
    void Start()
    {
        propType = PropType.Unuse;
        isRefresh = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (isRefresh)
        {
            StartCoroutine(StartRefresh());
        }
    }

    IEnumerator StartRefresh()
    {
        GameObject[] emptyBox = GameObject.FindGameObjectsWithTag("EmptyBox");
        List<Sprite> sprites = new List<Sprite>();
        for (int i = 0;i<emptyBox.Length; i++)
        {
            if (emptyBox[i].GetComponent<ItemController>().isUsed)
            {
                continue;
            }
            sprites.Add(emptyBox[i].transform.GetChild(0).GetComponent<SpriteRenderer>().sprite);
        }
        for (int i = sprites.Count - 1; i > 0;i--)
        {
            int exchange = Random.Range(0, i + 1);
            Sprite tmp = sprites[i];
            sprites[i] = sprites[exchange];
            sprites[exchange] = tmp;
        }
        for(int i = 0;i<emptyBox.Length;i++)
        {
            if (emptyBox[i].GetComponent<ItemController>().isUsed)
            {
                continue;
            }
            emptyBox[i].transform.GetChild(0).GetComponent<SpriteRenderer>().sprite = sprites[i];
        }
        yield return new WaitForSeconds(1.5f);
        isRefresh = false;
    }

    public void OnClickProp(int idx)
    {
        if(propType != PropType.Unuse)
        {
            return;
        }
        switch (idx)
        {
            case 1:
                if(removePropCount == 0){
                    propType = PropType.Remove;
                    popView.SetActive(true);
                    removeView.SetActive(true);
                    one.SetActive(false);
                    two.SetActive(false);
                }
                else
                {
                    OnUseRemoveProp();
                }
                break;
            case 2:
                if (undoPropCount == 0)
                {
                    propType = PropType.Undo;
                    popView.SetActive(true);
                    undoView.SetActive(true);
                    one.SetActive(false);
                    two.SetActive(false);
                }
                else
                {
                    OnUseUndoProp();
                }
                break;
            case 3:
                if (refreshPropCount == 0)
                {
                    propType = PropType.Refresh;
                    popView.SetActive(true);
                    refreshView.SetActive(true);
                    one.SetActive(false);
                    two.SetActive(false);
                }
                else
                {
                    OnUseRefreshProp();
                }
                break;
            default:
                propType = PropType.Unuse;
                break;
        }
    }

   

    public void OnGetProp()
    {
        switch (propType)
        {
            case PropType.Remove:
                removePropCount = 1;
                removePlus.GetComponent<Image>().sprite = oneSprite;
                break;
            case PropType.Undo:
                undoPropCount = 1;
                undoPlus.GetComponent<Image>().sprite = oneSprite;
                break;
            case PropType.Refresh:
                refreshPropCount = 1;
                refreshPlus.GetComponent<Image>().sprite = oneSprite;
                break;
        }
        propType = PropType.Unuse;
        popView.SetActive (false);
        one.SetActive(false);
        two.SetActive(true);
    }
    public void OnGiveUpProp()
    {
        switch (propType)
        {
            case PropType.Remove:
               removeView.SetActive(false);
                break;
            case PropType.Undo:
                undoView.SetActive(false);
                break;
            case PropType.Refresh:
                refreshView.SetActive(false);
                break;
        }
        propType = PropType.Unuse;
        popView.SetActive(false);
        one.SetActive(false);
        two.SetActive(true);
    }
    public void OnUseRemoveProp()
    {
        removePropCount = 0;
        removePlus.GetComponent<Image>().sprite = plusSprite;
        removemask.SetActive(true);
        removeView.SetActive(false);
        propType = PropType.Unuse;
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < propLayer.transform.childCount; j++)
            {
                if (!propLayer.transform.GetChild(j).gameObject.activeSelf)
                {
                    propLayer.transform.GetChild(j).gameObject.SetActive(true);
                    propLayer.transform.GetChild(j).GetChild(0).GetComponent<SpriteRenderer>().sprite = SlotView.Instance.loadingSprites[i];
                    SlotView.Instance.loadingSprites[i] = null;
                    break;
                }
            }
        }
        for(int i = 3; i < SlotView.Instance.loadingSprites.Length; i++)
        {
            SlotView.Instance.loadingSprites[i-3] = SlotView.Instance.loadingSprites[i];
            SlotView.Instance.loadingSprites[i] = null;
        }
    }
    public void OnUseUndoProp()
    {
        undoPropCount = 0;
        undoPlus.GetComponent<Image>().sprite = plusSprite;
        undomask.SetActive(true);
        undoView.SetActive(false);
        propType = PropType.Unuse;
        EmptyBox emptyBox = SlotView.Instance.emptyBox;
        GameObject newEmptyBox = Instantiate(emptyBoxPrefab, emptyBox.pos, Quaternion.identity);
        newEmptyBox.transform.SetParent(emptyBox.parent);
        newEmptyBox.transform.GetChild(0).GetComponent<SpriteRenderer>().sprite = emptyBox.sprite;
        newEmptyBox.transform.GetChild(0).GetComponent<SpriteRenderer>().sortingLayerName="Layer"+emptyBox.layerNum;
        newEmptyBox.GetComponent<SpriteRenderer>().sortingLayerName = "Layer" + emptyBox.layerNum;
        newEmptyBox.GetComponent<ItemController>().layerNum = emptyBox.layerNum;
        SlotView.Instance.loadingSprites[emptyBox.slotIdx] =null;
        for (int i = emptyBox.slotIdx + 1; i < SlotView.Instance.loadingSprites.Length; i++)
        {
            if(SlotView.Instance.loadingSprites[i] == null)
            {
                break;
            }
            SlotView.Instance.loadingSprites[i-1]=SlotView.Instance.loadingSprites[i];
            SlotView.Instance.loadingSprites[i] = null;
        }
    }
    public void OnUseRefreshProp()
    {
        refreshPropCount = 0;
        refreshPlus.GetComponent<Image>().sprite = plusSprite;
        refresgmask.SetActive(true);
        refreshView.SetActive(false);
        propType = PropType.Unuse;
        isRefresh = true;
    }
}
